package com.baimifan.renovation;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.Window;

import com.baimifan.renovation.tools.MyrootDialog;
import com.baimifan.renovation.tools.PermissionsChecker;


/**
 * 授权页展示
 */
public class PermissionsActivity extends Activity {
    public static final int PERMISSIONS_GRANTED = 0; // 权限授权
    public static final int PERMISSIONS_DENIED = 1; // 权限拒绝
    private static final int PERMISSION_REQUEST_CODE = 0; // 系统权限管理页面的参数
    private PermissionsChecker mChecker; // 权限检测器
    private boolean isRequireCheck; // 是否需要系统权限检测
    private static final String EXTRA_PERMISSIONS = "permission";
    private static final String PACKAGE_URL_SCHEME = "package:"; // 方案
    private MyrootDialog dialog;

    public static void startActivityForResult(Activity activity, int requestCode, String... permissions) {
        Intent intent = new Intent(activity, PermissionsActivity.class);
        intent.putExtra(EXTRA_PERMISSIONS, permissions);
        ActivityCompat.startActivityForResult(activity, intent, requestCode, null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_permissions);
        if(getIntent() == null || !getIntent().hasExtra(EXTRA_PERMISSIONS)){
            throw  new RuntimeException("");
        }
        mChecker = new PermissionsChecker(this);
        isRequireCheck = true;

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isRequireCheck){
            String[] permissions = getPermissions();
            if(mChecker.lacksPermissions(permissions)){
                requestPermissions(permissions);
            }else{
                allPermissionsGranted();
            }
        }else{
            isRequireCheck = true;
        }
    }

    // 返回传递的权限参数
    private String[] getPermissions() {
        return getIntent().getStringArrayExtra(EXTRA_PERMISSIONS);
    }

    // 请求权限兼容低版本
    private void requestPermissions(String... permissions) {
        ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE);
    }

    // 全部权限均已获取
    private void allPermissionsGranted() {
        setResult(PERMISSIONS_GRANTED);
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)) {
            isRequireCheck = true;
            allPermissionsGranted();
        } else {
            isRequireCheck = false;
            showMissingPermissionDialog();
        }
}

    // 含有全部的权限
    private boolean hasAllPermissionsGranted(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }

    // 显示缺失权限提示
    private void showMissingPermissionDialog() {
        dialog = new MyrootDialog(PermissionsActivity.this,getResources().getString(R.string.string_help_text),
                new MyrootDialog.DialogListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.root_confirm:
                        dialog.dismiss();
                        setResult(PERMISSIONS_DENIED);
                        finish();
                        break;

                    case R.id.root_cancel:
                        startAppSettings();
                        dialog.dismiss();
                        break;
                }
            }
        });
        dialog.show();
    }

    // 启动应用的设置
    private void startAppSettings() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));
        startActivity(intent);
    }
}
